import {createRouter, createWebHistory} from 'vue-router'
import {ElMessage} from "element-plus";
import {getToken} from "@/utils/tools/auth";
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import {useUserStore} from "@/store/modules/user";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/index/home'
    },
    {
      path: '/404',
      name: '404',
      component: () => import(/* webpackChunkName: "about" */ '@/views/404.vue'),
      meta:{
        title:'找不到网页啦'
      }
    },
    {
      path: '/index',
      name: 'index',
      redirect: '/index/home',
      component: () => import(/* webpackChunkName: "about" */ '@/views/Index.vue'),
      meta:{"title":"阿伟博客"},
      children:[
         {path:'/index/home',name: 'Home',component: () => import(/* webpackChunkName: "about" */ '@/views/page/Container.vue'),meta:{title:"首页"},
           // children:[{
           //   path:'/index/home/article',name: 'Article',component: () => import(/* webpackChunkName: "about" */ '@/views/container/tables/article.vue'),meta:{title:"首页"},
           // }]
         },
        { path:'/index/personal',name: 'Personal',component: () => import(/* webpackChunkName: "about" */ '@/views/page/Personal.vue'),meta:{title:"个人中心"}},
        { path:'/index/article/detail',name:"ArticleDetail",component: () => import(/* webpackChunkName: "about" */ '@/views/page/ArticleDetail.vue'),meta:{title:"文章详情"}},
        { path:'/index/article/publish',name:"PublishArticle",component: () => import(/* webpackChunkName: "about" */ '@/views/page/PublishArticle.vue'),meta:{title:"发表文章"}}
      ]
    },
  ]
})

NProgress.configure({ showSpinner: false })
const whiteList = ['/index',"/auth","/empty","/index/home","/index/home/article","/index/article/detail"];  //白名单 表示可以匿名访问
const tokenList = ['/auth']; //验证通过可以跳过的页面
const adminList =["/home","/home/index","/home/user","/home/article","/home/courses","/home/swagger"]; //管理员 只有管理员才可以访问
router.beforeEach((to, from, next) =>{
  const store = useUserStore();
  NProgress.start();
  // 设置页面标题
  document.title = (to.meta.title ? to.meta.title : '');
    if (to.matched.length === 0) {
       next('/404');
      }
    // 判断是否登录
    let token =getToken();
    if (token !=null){
      next();
      store.getUser().then(res=>{
          //验证token通过,可以跳过的页面
          if (tokenList.includes(to.path)){
            //直接到/home
            next({
              path:'/home/index'
            })
            NProgress.done();
          }
          //只有管理员才能进入的页面
          if (adminList.includes(to.path)){
            //不是管理员的情况
               if(res.data !== 'admin'){
                 ElMessage.info("暂无权限")
                 //直接到/home
                 next({
                   path:'/index/home'
                 })
               }
          }
          NProgress.done();
      }).catch(()=>{
        //登录状态已过期的情况下
        NProgress.done();
        store.clearStore();
      })
    } //没有token
    else {
      if (store.status){
        store.clearStore();
        ElMessage.info("登录过期")
      }
      // 判断是否访问白名单
      if (whiteList.includes(to.path)){
         next()
        NProgress.done();
      }else {
        //没有token时应该修改登录状态
        ElMessage.info("请先登录")
        next({
          path:'/index'
        })
        NProgress.done();
      }
    }
})

export default router
